import {  createRouter, createWebHistory } from "vue-router"
import auth from "./utils/auth.js"


const routes = [
  {
    path: '/',
    component: () => import('./components/Frame.vue'),
    children:[
      {
        path: '',
        name: 'index',
        component: () => import('./components/Index.vue')
      },
      {
        path: 'project',
        name: 'project',
        component: () => import('./components/Project/ProjectList.vue')
      },
      {
        path: 'project/:project_id',
        name: 'project_detail',
        component: () => import('./components/Project/ProjectDetail.vue')
      },
      {
        path: 'members',
        name: 'members',
        component: () => import('./components/MemberList.vue')
      },
      {
        path: 'settings',
        name: 'settings',
        component: () => import('./components/Settings.vue')
      },
      {
        path: 'tools',
        name: 'tools',
        component: () => import('./components/Tools.vue')
      }
    ]
    },{
        path: '/login',
        name: 'login',
        component: () => import('./components/Login.vue')    // component: import('../views/reg.vue')
      }
]

  
const router = createRouter({
  history: createWebHistory(),
  routes
})

//路由拦截，如果访问url不是login且用户没有登录，跳转到登陆页面
router.beforeEach((to, from, next) => {
  if (to.name != 'login' && !auth.is_authed) {
    next({
      name: "login"
    })
  } else {
    next()
  }
})
export default router